

//点击头部跳转首页
$(".headerUl li").each(function(i,n){
    $(n).click(function(){
        window.location.href="../index.html"
    })
})

function Page(ele, opt) {
    this.pageIndex = 1;
    this.option = {
        total: 100,//数据总数
        shownum: 50,//每页显示的数
        showpage: 5,//连续出现的页码数
        callBack: function () {

        }
    }
    this.target = document.querySelector(ele);
    this.extend(opt);
    this.create();//生成结构
    this.bindData();//绑定数据

}
Page.prototype.extend = function (opt) {
    for (var i  in opt) {
        this.option[i] = opt[i];
    }
}
Page.prototype.bindEvent = function () {
    var that = this;
    this.prevBtn.className = "page-prev";
    this.prevBtn.onclick = function () {
        that.pageIndex--;
        that.bindData();
    }
    this.nextBtn.className = "page-next";
    this.nextBtn.onclick = function () {
        that.pageIndex++;
        that.bindData();
    }

}
Page.prototype.bindData = function () {
  
    var that = this;
    var allPage = Math.ceil(this.option["total"] / this.option["shownum"]);//获取所有的页码总数 10
    var middleNum = Math.floor(this.option["showpage"] / 2);// 2
    var start = 1;
    var end = this.option["showpage"]//默认的开始和结束 5
    end = allPage > end ? end : allPage;//页码处理

    start = this.pageIndex > middleNum ? this.pageIndex - middleNum : start; //起点
    end = this.pageIndex > middleNum ? this.pageIndex + middleNum : end;//中点
    //难点 在于页码的算法
    end = this.pageIndex > (allPage - middleNum) ? allPage : end;//大于8 终点就是10  起点?
    start = this.pageIndex > (allPage - middleNum) ? allPage - middleNum * 2 : start;
    start = start < 1 ? 1 : start;//处理数据特别少的时候

    this.content.innerHTML = "";
    for (var i = start; i <= end; i++) {
        var li = document.createElement("li");
        li.innerHTML = i;
        if (i == this.pageIndex) {   
            li.className="select";
        }
        this.content.appendChild(li);
        li.onclick = function () {
            that.pageIndex = this.innerHTML * 1;//设置页码
            that.bindData();//当页码发生改变的时候 重新生成
           
        }

    }
    this.bindEvent(); //先绑定 在取消
    if (this.pageIndex == 1) {//第一页的时候上一页不能使用
        this.prevBtn.onclick = null;
        this.prevBtn.className = "page-prev disabled";
    }
    if (this.pageIndex == allPage) {
        this.nextBtn.onclick = null;
        this.nextBtn.className = "page-next disabled";
    }
    if (this.option["callBack"]) {
        this.option["callBack"](this.pageIndex);
    }


}
Page.prototype.create = function () {
    this.target.innerHTML = "";
    this.target.className = "page";
    this.prevBtn = document.createElement("span");
    this.prevBtn.className = "page-prev";
    this.prevBtn.innerHTML = "上一页";
    this.target.appendChild(this.prevBtn);

    this.content = document.createElement("ul");
    this.content.className = "page-content";
    this.target.appendChild(this.content);

    this.nextBtn = document.createElement("span");
    this.nextBtn.className = "page-next";
    this.nextBtn.innerHTML = "下一页";
    this.target.appendChild(this.nextBtn);

}

function myPage(ele, json) {
    return new Page(ele, json);
}






// 获取全部数据条数
var shownum=8;
ajaxPage();
function ajaxPage(){
    $.ajax({
        type: "get",
        url: "../php/fenyecount.php",
        dataType: "json",
        success: function (count) {
            myPage("#fenyechajian", {
                total: count[0],//数据总数
                shownum: 8,//每页显示的数
                showpage: 5,//连续出现的页码数
                callBack: function (pageIndex) {
                    // alert(pageIndex)
                    var page=(pageIndex-1)*shownum;
                    if($(".ctNav .selected").length==1){
                        var paixu=$(".ctNav .selected").attr("paixu");
                    }else{
                        var paixu=""
                    }
                   
                    data(paixu,"../php/jutifenye.php",shownum,page); //执行了  
                }
            })
        }
    });
}

data("","../php/fenye.php");
//排序
$("#di").click(function(){
    $("#gao").removeClass("selected");
    $(".goodsbox").empty();
    var paixu=$(this).attr("paixu");
    $(this).addClass("selected");
    data(paixu,"../php/fenye.php");
    ajaxPage();
})
$("#gao").click(function(){
    $("#di").removeClass("selected");
    $(".goodsbox").empty();
    var paixu=$(this).attr("paixu");
    $(this).addClass("selected");
    data(paixu,"../php/fenye.php");
    ajaxPage();
})

//点击跳转到详情页
$(document).on("click",".goodsbox li",function(i,n){
    var id=$(this).attr("data-id");
    window.location.href="../html/shoppingcar.html?id="+id;
})

//全部内容
function data(paixu,url,shownum,page,){
    $.ajax({
        type: "get",
        url: url,
        dataType: "json",
        data:{"paixu":paixu,"shownum":shownum,"page":page},
        success: function (list) {
            $(".goodsbox").empty();
            var html=""
            $.each(list,function(i,n){
                var id=n.id;
                var smallimg=n.smallimg;
                var goodsname=n.goodsname;
                var price=n.price;
                html+=`
                    <li class="goodsLi" data-id=${id}>
                        <img src="../images/${smallimg}" alt="">
                        <p>${goodsname}</p>
                        <div><span class="iconfont icon-qian">${price}</span></div>
                    </li>
                `  
            })
            $(".goodsbox").append(html);
        }
    });
}

//登录
$("#login").click(function(){
    var url=window.location.href;
    setCookie("backUrl",url,7);
    window.location.href="../html/login.html";
})
$("#join").click(function(){
    var url=window.location.href;
    setCookie("backUrl",url,7);
    window.location.href="../html/join.html";
})

var usertel=getCookie("logintel");
if(usertel){
    $.ajax({
        type: "get",
        url: "../php/num.php",
        data: {"usertel":usertel},
        dataType: "json",
        success: function (item) {
            if(item[0]>0){
                $("#shoppingcar").html(item[0]);
                $("#shoppingcar").css("display","block");
            }
        }
    });
}

$(".icon-fanhuidingbu").click(function () { 
    $("html, body").animate({scrollTop: 0}, 1500); /* 持续时间为 100ms */
    return false;
});

$(".icon-tubiaolunkuo-").click(function(){
    window.location.href="../html/shopping.html";
})
//登录以后小人里面放用户电话号码
$(".icon-gerenxinxi").hover(
    function(){
        if(getCookie("logintel")){
            $(".xinxiboxLi").html(getCookie("logintel"))
        }
    }
)
//模糊查询
$(".icon-sousuo").click(function(){
    var neirong=$(".headerp1").val();
    window.location.href="../html/search.html?neirong="+neirong;
   
})
